<?php

if (strtoupper(substr(PHP_OS, 0, 3)) === 'WIN') {
 	require_once (APPLICATION_PATH . "\\models\\connectionDb.php");
} else {
    require_once (APPLICATION_PATH . "//models//connectionDb.php");
}

class Application_Model_DbTable_Groups extends Zend_Db_Table_Abstract
{

    protected $_name = 'Groups';
	protected $_primary = 'id';
	
	public function getGroup($id)
    {
    	try{
	    	$id = (int)$id;	    	
			$row = $this->fetchRow('id = ' . $id);
			if (!$row) {
				return null;
			}
			return $row->toArray();
    	}
    	catch (Zend_Exception $e) {  
			echo "Error message: " . $e->getMessage() . "\n";
        } 
    }
	
	
	public function AddGroup($name,$description, $info,$groupCreator)
    {
    	try{
	    	$data = array(
	    		'Name' => $name,
	    		'Description' => $description,
	    		'Info' => $info,
	    		'GroupCreator_id' => $groupCreator,
	    	);
	    	return $this->insert($data);
    	}
    	catch (Zend_Exception $e) {  
			echo "Error message: " . $e->getMessage() . "\n";
        } 
    }
    
	public function getSearchGroups($group_name)
    {
    	try{
    		
    		$sql ="select Groups.*, Users.username";
			$sql .=" from Groups";
			$sql .=" inner join Users on Users.id = Groups.GroupCreator_id";			
			$sql .=" where 1=1";
			$sql .=" and Groups.Name Like '%".$group_name."%'";
			
    		
    		$db = connectionDb::connectDB();		
			$rows = $db->fetchAll($sql);			
	  					 
	    	if (!$rows) {
	   			return null;
	  		}
	  		return $rows;
    	
    	}
    	catch (Zend_Exception $e) {  
			echo "Error message: " . $e->getMessage() . "\n";
        }
    }
	public function getGroupPosts($group_id)
    {
    	try{
    		
    		$sql =" select GroupPosts.*,Users.*,BasicInfo.FirstName,BasicInfo.LastName";
			$sql .=" from GroupPosts";
			$sql .=" inner join Users on Users.id = GroupPosts.User_id";
			$sql .=" inner join BasicInfo on BasicInfo.id = Users.BasicInfo_id";			
			$sql .=" where 1=1";
			$sql .=" and GroupPosts.Group_id=".$group_id;
			$sql .=" ORDER BY GroupPosts.DatePost desc ";
			    		
    		$db = connectionDb::connectDB();		
			$rows = $db->fetchAll($sql);			
	  					 
	    	if (!$rows) {
	   			return null;
	  		}
	  		return $rows;
    	
    	}
    	catch (Zend_Exception $e) {  
			echo "Error message: " . $e->getMessage() . "\n";
        }
    }
	public function getGroupforUser($user_id)
    {
    	try{
    		
    		$sql =" select GroupPosts.*";
			$sql .=" from GroupPosts";				
			$sql .=" where 1=1";
			$sql .=" and GroupPosts.User_id=".$user_id;			    		
    		$db = connectionDb::connectDB();		
			$rows = $db->fetchAll($sql);			
	  					 
	    	if (!$rows) {
	   			return null;
	  		}
	  		return $rows;
    	
    	}
    	catch (Zend_Exception $e) {  
			echo "Error message: " . $e->getMessage() . "\n";
        }
    }

	public function getLastGroupCreated()
    {
    	try{    		
    		$sql =" select id";
			$sql .=" from Groups";				
			$sql .=" ORDER BY id DESC";
			$sql .=" LIMIT 1";			    		
    		$db = connectionDb::connectDB();		
			$rows = $db->fetchAll($sql);			
	  					 
	    	if (!$rows) {
	   			return null;
	  		}
	  		return $rows;
    	
    	}
    	catch (Zend_Exception $e) {  
			echo "Error message: " . $e->getMessage() . "\n";
        }
    }
}

